package com.gem.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.back.entity.AoaSysMenu;
import com.gem.back.mapper.AoaSysMenuMapper;
import com.gem.back.service.IAoaSysMenuService;
import com.gem.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Service
public class AoaSysMenuServiceImpl extends ServiceImpl<AoaSysMenuMapper, AoaSysMenu> implements IAoaSysMenuService {
    @Autowired
    AoaSysMenuMapper aoaSysMenuMapper;
    @Override
    public List<AoaSysMenu> findListByName(String username) {
        return aoaSysMenuMapper.findByUserName(username);
    }

    @Override
    public List<Map<String, Object>> queryMenuTree() {
        List<AoaSysMenu> sysMenus = aoaSysMenuMapper.findAllMenu();

        //rootMenu存放根菜单
        List<Map<String, Object>> rootMenu = new ArrayList<>();
        //取出pid为0的节点
        if (!CollectionUtils.isEmpty(sysMenus)) {
            List<AoaSysMenu> parentList = sysMenus.stream().filter(item -> 0 == (item.getParentId())).collect(Collectors.toList());
            //把根节点放在Mpa里面，并去调用递归算法去查询子树
            if (!CollectionUtils.isEmpty(parentList)) {
                parentList.forEach(item -> {
                    Map<String, Object> map = new HashMap<>();
                    packageData(item, map);
                    //查询出子节点
                    this.querySubMenuTree(map, sysMenus);
                    rootMenu.add(map);
                });
            }

        }
        return rootMenu;
    }

    @Override
    public AoaSysMenu queryMenuById(Integer menuId) {
        return aoaSysMenuMapper.selectById(menuId);
    }

    @Override
    public AoaSysMenu queryParentMenuById(Integer menuId) {
        AoaSysMenu sysMenu = aoaSysMenuMapper.selectById(menuId);
        return aoaSysMenuMapper.selectList(new QueryWrapper<AoaSysMenu>()
                .eq("menu_id", sysMenu.getParentId() == 0 ? menuId : sysMenu.getParentId())).get(0);
    }

    @Override
    public void saveMenu(AoaSysMenu sysMenu) {
        if (sysMenu.getMenuId() != null) {
            aoaSysMenuMapper.updateById(sysMenu);
        } else {
            if (sysMenu.getParentId() == null) {
                sysMenu.setParentId(0);
            }
            aoaSysMenuMapper.insert(sysMenu);
        }
    }

    @Override
    public ResponseResult deleteMenu(Integer menuId) {
        //查询是否含有子菜单，有则删除失败
        List<AoaSysMenu> subCatalog = aoaSysMenuMapper.selectList(new QueryWrapper<AoaSysMenu>()
                .eq("parent_id", menuId));
        if (!subCatalog.isEmpty()) {
            return new ResponseResult("201", "请先删除子菜单！");
        }

        //开始删除菜单
        aoaSysMenuMapper.deleteById(menuId);

        return new ResponseResult("200", "删除成功！");
    }

    /**
     * 获取子菜单
     */
    public Map<String, Object> querySubMenuTree(Map<String, Object> rootMap, List<AoaSysMenu> sysMenus) {
        //subMenu
        List<Map<String, Object>> subMenu = new ArrayList<>();

        //选出某一个根节点下面的子树，递归。
        List<AoaSysMenu> sub = sysMenus.stream().filter(item -> rootMap.get("id") == (item.getParentId())).collect(Collectors.toList());
        //取出pid为0的节点
        if (!sub.isEmpty()) {
            sub.forEach(item -> {
                Map<String, Object> subMap = new HashMap<>();
                packageData(item, subMap);
                //查询出子节点
                subMenu.add(subMap);
                this.querySubMenuTree(subMap, sysMenus);
            });
        }

        if (!CollectionUtils.isEmpty(subMenu)) {
            rootMap.put("children", subMenu);
        }
        return rootMap;
    }

    /**
     * 封装菜单数据
     *
     * @param sysMenu
     * @param map
     */
    private void packageData(AoaSysMenu sysMenu, Map<String, Object> map) {
        map.put("id", sysMenu.getMenuId());
        map.put("name", sysMenu.getMenuName());
        map.put("href", sysMenu.getMenuUrl());
//        map.put("sort", sysMenu.getSort());
    }
}
